*==========================================================================================================================================================
*
*	Local Projections
*
*==========================================================================================================================================================


set more off

qui {

*-------------------------------------------------------------------------------------------------------------
*	SETTINGS
*-------------------------------------------------------------------------------------------------------------

* Variables
local vars "gdp_r_adj cpi_sil log_R_esp_er_hp itsN wage_sil" 
local lava ""Real output" "Consumer prices" "Lending rate" "Money inflow (% of pre-disaster stock)" "Nominal wage"" 
local yti ""Percent" "Percent" "Percentage pts" "Percentage pts" "Percent""

* irf horizon
local H 5
local F 5

* lags
local L 2
local LS `L'

*-------------------------------------------------------------------------------------------------------------

local count = 0
foreach v of local vars {
local count = `count'+1
n di "Variable `v'"


*_________ GET DATA ____________
use "data/working files/prepared.dta", clear 



*____ DECLARE TIME SERIES _____
gen id =1
tsset id year, yearly


*_________________ LOCAL PROJECTIONS _________________

***_________ CONTROLS

local indep	" gdp_r_adj_gr cpi_sil_gr wage_sil_gr itsN_gr" 

forvalues i=0/`L'{
	local indep_`i' = subinstr("`indep'"," "," l`i'_",.)
	}
	
local rhs ""
forvalues i = 1/`L'{
	local rhs "`rhs'`indep_`i''"
	}
	
	local not l0_`v'_gr l0_itsN_gr 
	local rhs: list rhs- not
	
	


	***_________ IMPULSE RESPONSE and CONFIDENCE INTERVAL variables
	gen irf_`v' = . if _n<=`H'+1
	
	gen se_`v' = . if _n<=`H'+1

	gen ci_up_`v' = . if _n<=`H'+1
	gen ci_lo_`v' = . if _n<=`H'+1
	gen ci_up90_`v' = . if _n<=`H'+1
	gen ci_lo90_`v' = . if _n<=`H'+1
			
	
	if(`"`v'"'=="gdp_r_an"){
		gen irf_apr_r_ind = . if _n<=`H'+1
		gen se_apr_r_ind = . if _n<=`H'+1
		gen ci_up_apr_r_ind = . if _n<=`H'+1
		gen ci_lo_apr_r_ind = . if _n<=`H'+1
		gen ci_up90_apr_r_ind = . if _n<=`H'+1
		gen ci_lo90_apr_r_ind = . if _n<=`H'+1
		}
		

	
	***_________ ESTIMATE COEFFICIENTS
	
	forvalues y = 0/`H' {
		n display "projection `y'"

		newey `v'_gr`y' l(1/`LS').`v'_gr l(-`F'/`LS').perclossN_vellon l(-`F'/`LS').perclossN `rhs' l(-`F'/`LS').percloss_salvN l(-`F'/`LS').percloss_pirN l(0/`LS').temperature l(0/`LS').warbeu l(0/`LS').warbciv, lag(5) force  
		
		* store IMPULSE RESPONSES and STANDARD ERRORS
		lincom perclossN  //l0_perclossN -itsN_gr
		replace irf_`v' = r(estimate) if _n==`y'+1 /* coefficients */
		replace se_`v' = r(se) if _n==`y'+1 /* standard errors */

		} //horizon			


	***_________ CONFIDENCE INTERVALS
	replace ci_up_`v' = irf_`v' + 1*se_`v' if _n <= `H'+1
	replace ci_lo_`v' = irf_`v' - 1*se_`v' if _n <= `H'+1
	replace ci_up90_`v' = irf_`v' + 1.64*se_`v' if _n <= `H'+1
	replace ci_lo90_`v' = irf_`v' - 1.64*se_`v' if _n <= `H'+1
	

* plot prerequisites
gen time = _n-1 if(_n<=`H'+1) //IRF horizon
drop if time > `H'
keep time irf_* ci_up_* ci_lo_* ci_up90_* ci_lo90_* se_*
order irf_* ci_up_* ci_lo_* time ci_up90_* ci_lo90_*
drop if time == . 
gen zero = 0 if time!=.

local title : word `count' of `lava'
local ytitle : word `count' of `yti'


sum ci_up90_`v'
local max = round(`r(max)'*1.2,0.5)
if (`max'<0) {
	local max = 0.5
	}

sum ci_lo90_`v'
local min = round(`r(min)'*1.33,0.5)


//text position

if (`"`v'"'=="log_R_esp_er_hp") {
	local lsize = 6
	local tsize = 6.5
twoway  (rarea ci_lo90_`v' ci_up90_`v' time, fcolor(gs15) lcolor(gs15)) ///
		(rarea ci_lo_`v' ci_up_`v' time, fcolor(gs12) lcolor(gs12)) ///
		(line irf_`v' time, lcolor(black) lpattern(solid) lwidth(medthick)) ///
		(line zero time, lcolor(black) lwidth(medium)), title("") xtitle("Year", size(`lsize')) ytitle(`ytitle', height(3) size(`lsize')) xlabel(,labsize(`lsize')) ylabel(`min'(1)`max', format(%9.1f) nogrid angle(0) labsize(`lsize')) ///
		legend(off) title(`title', size(`tsize')) ///
		graphregion(color(white)) plotregion(color(white)) name(g`count', replace) nodraw
	}


	
if (`"`v'"'=="gdp_r_adj")  { 
	local lsize "vlarge"
	local tsize "vlarge"
twoway  (rarea ci_lo90_`v' ci_up90_`v' time, fcolor(gs15) lcolor(gs15)) ///
		(rarea ci_lo_`v' ci_up_`v' time, fcolor(gs12) lcolor(gs12)) ///
		(line irf_`v' time, lcolor(black) lpattern(solid) lwidth(medthick)) ///
		(line zero time, lcolor(black) lwidth(medium)), title("") xtitle("Year", size(`lsize')) ytitle(`ytitle', height(3) size(`lsize')) xlabel(,labsize(`lsize')) ylabel(, format(%9.1f) nogrid angle(0) labsize(`lsize')) ///
		legend(off) title(`title', size(`tsize')) ///
		graphregion(color(white)) plotregion(color(white)) name(g`count', replace) nodraw
	}
	
	
if (`"`v'"'=="itsN")  { 
	local lsize "vlarge"
	local tsize "vlarge"
twoway  (rarea ci_lo90_`v' ci_up90_`v' time, fcolor(gs15) lcolor(gs15)) ///
		(rarea ci_lo_`v' ci_up_`v' time, fcolor(gs12) lcolor(gs12)) ///
		(line irf_`v' time, lcolor(black) lpattern(solid) lwidth(medthick)) ///
		(line zero time, lcolor(black) lwidth(medium)), title("") xtitle("Year", size(`lsize')) ytitle(`ytitle', height(3) size(`lsize')) xlabel(,labsize(`lsize')) ylabel(-1 0, format(%9.1f) nogrid angle(0) labsize(`lsize')) ///
		legend(off) title(`title', size(`tsize')) ///
		graphregion(color(white)) plotregion(color(white)) name(g`count', replace) nodraw
	}
	
if (`"`v'"'=="cpi_sil" | `"`v'"'=="wage_sil")  { 
	local lsize = 6
	local tsize = 6.5
twoway  (rarea ci_lo90_`v' ci_up90_`v' time, fcolor(gs15) lcolor(gs15)) ///
		(rarea ci_lo_`v' ci_up_`v' time, fcolor(gs12) lcolor(gs12)) ///
		(line irf_`v' time, lcolor(black) lpattern(solid) lwidth(medthick)) ///
		(line zero time, lcolor(black) lwidth(medium)), title("") xtitle("Year", size(`lsize')) ytitle(`ytitle', height(3) size(`lsize')) xlabel(,labsize(`lsize')) ylabel(, format(%9.1f) nogrid angle(0) labsize(`lsize')) ///
		legend(off) title(`title', size(`tsize')) ///
		graphregion(color(white)) plotregion(color(white)) name(g`count', replace) nodraw
	}
	


}



graph combine g4 g1, name(lp_real, replace) cols(2) imargin(medsmall) plotregion(color(white)) graphregion(color(white)) 
graph display lp_real, ysize(3) xsize(6) 
graph export "results/Figure2.pdf", replace 

graph combine g3 g2 g5, name(lp_trans, replace) cols(3) imargin(med) plotregion(color(white)) graphregion(color(white)) 
graph display lp_trans, ysize(2.5) xsize(6) 
graph export "results/Figure4.pdf", replace 




} // qui


*_________________________________________

			
